<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>「状压DP」P1441 题解 砝码称重 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="一篇题解，用的是状压DP的思路。">
<meta property="og:type" content="article">
<meta property="og:title" content="「状压DP」P1441 题解 砝码称重">
<meta property="og:url" content="http://example.com/2020/08/06/%E3%80%8C%E7%8A%B6%E5%8E%8BDP%E3%80%8DP1441%20%E9%A2%98%E8%A7%A3%20%E7%A0%9D%E7%A0%81%E7%A7%B0%E9%87%8D/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="一篇题解，用的是状压DP的思路。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-06T06:36:51.300Z">
<meta property="article:modified_time" content="2023-07-19T11:50:05.029Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-「状压DP」P1441 题解 砝码称重" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/08/06/%E3%80%8C%E7%8A%B6%E5%8E%8BDP%E3%80%8DP1441%20%E9%A2%98%E8%A7%A3%20%E7%A0%9D%E7%A0%81%E7%A7%B0%E9%87%8D/" class="article-date">
  <time class="dt-published" datetime="2020-08-06T06:36:51.300Z" itemprop="datePublished">2020-08-06</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      「状压DP」P1441 题解 砝码称重
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>一篇题解，用的是状压DP的思路。</p>
<p><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/blog/ztx666-blog/zhuang-tai-ya-su-dong-tai-gui-hua-zhuang-ya-dp-yang-xie">前置知识：状压DP</a></p>
<p><a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1441">洛谷传送门</a></p>
<p>emm….看到题目，我第一个想到的就是<strong>枚举</strong>。<del>暴力大法好！</del></p>
<p>具体怎么枚举？当然是子集枚举啦！<strong>枚举出每一个可能的砝码选择方案。对于每一个合法的（也就是选取数量等于$n-m$的）方案，求出这个方案能称出重量的数量。至于如何求重量的数量，枚举出这个方案所有的子方案，再对每个子方案的和去重。</strong></p>
<p><del>这个方法实在是太暴力了</del></p>
<p>Code：<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 22</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXA 2005</span></span><br><span class="line"><span class="type">int</span> n,m,a[MAXN],ans,sum[<span class="number">1</span>&lt;&lt;<span class="number">22</span>],cnt[<span class="number">1</span>&lt;&lt;<span class="number">22</span>];</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,a+i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;(<span class="number">1</span>&lt;&lt;n);i++)&#123;<span class="comment">//状压</span></span><br><span class="line">        <span class="type">int</span> high_bit=<span class="number">0</span>,high_bit_num=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">31</span>;j&gt;=<span class="number">0</span>;j--)&#123;</span><br><span class="line">            <span class="keyword">if</span>((i&gt;&gt;j)&amp;<span class="number">1</span>)&#123;</span><br><span class="line">                high_bit=<span class="number">1</span>&lt;&lt;j;</span><br><span class="line">                high_bit_num=j;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="comment">//求出当前方案的最高位</span></span><br><span class="line">        sum[i]=sum[i^high_bit]+a[high_bit_num+<span class="number">1</span>];<span class="comment">//转移。因为i是“按顺序”枚举的，所以去掉最高位后的方案一定枚举过了。</span></span><br><span class="line">        cnt[i]=cnt[i^high_bit]+<span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;(<span class="number">1</span>&lt;&lt;n);i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(cnt[i]==n-m)&#123;</span><br><span class="line">            set&lt;<span class="type">int</span>&gt; heavy;<span class="comment">//set有自动去重的功效</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=i;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span>((j|i)==i)&#123;</span><br><span class="line">                    heavy.<span class="built_in">insert</span>(sum[j]);<span class="comment">//枚举每个子方案</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            ans=<span class="built_in">max</span>(ans,<span class="built_in">int</span>(heavy.<span class="built_in">size</span>()));<span class="comment">//取大</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>但是，虽然暴力打得很爽，时间复杂度也非常爆炸。<strong>时间复杂度达到了可怕的$O((2^{n})^2)$！</strong> 所以，优化是必须的。</p>
<p>可以考虑对求重量的数量的过程进行优化。<strong>定义状态$dp(i)$代表当前方案能否称出重量$i$，$a(j)$代表当前考虑的砝码（当然，这个砝码必须包含在当前方案里），容易想出像下面这样的状态转移方程：</strong></p>
<script type="math/tex; mode=display">dp(i)=dp(i-a(1)) \operatorname{OR} dp(i-a(2))...\operatorname{OR} dp(i-a(n))</script><p>这个方程<del>翻译成人话</del>的意思就是：如果一个重量可以被组合出来，那么再加一个砝码也能被组合出来。反过来就是，如果一个重量减去一个砝码的重量能被组合出来，那么这个重量能够被组合出来。</p>
<p>有了状态转移方程，代码就非常好写了。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 22</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXA 2005</span></span><br><span class="line"><span class="type">int</span> n,m,a[MAXN],cnt[<span class="number">1</span>&lt;&lt;<span class="number">22</span>],dp[MAXA],ans;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,a+i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;(<span class="number">1</span>&lt;&lt;n);i++)&#123;<span class="comment">//状压</span></span><br><span class="line">        <span class="type">int</span> high_bit=<span class="number">0</span>,high_bit_num=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">31</span>;j&gt;=<span class="number">0</span>;j--)&#123;</span><br><span class="line">            <span class="keyword">if</span>((i&gt;&gt;j)&amp;<span class="number">1</span>)&#123;</span><br><span class="line">                high_bit=<span class="number">1</span>&lt;&lt;j;</span><br><span class="line">                high_bit_num=j;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="comment">//求出当前方案的最高位</span></span><br><span class="line">        cnt[i]=cnt[i^high_bit]+<span class="number">1</span>;<span class="comment">//转移。因为i是“按顺序”枚举的，所以去掉最高位后的方案一定枚举过了。</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;(<span class="number">1</span>&lt;&lt;n);i++)&#123;</span><br><span class="line">        <span class="keyword">if</span>(cnt[i]==n-m)&#123;</span><br><span class="line">            <span class="built_in">fill</span>(dp,dp+<span class="number">2000</span>+<span class="number">1</span>,<span class="literal">false</span>);</span><br><span class="line">            dp[<span class="number">0</span>]=<span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=n;j++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(i&gt;&gt;(j<span class="number">-1</span>)&amp;<span class="number">1</span>)&#123;<span class="comment">//如果当前砝码在方案里才考虑</span></span><br><span class="line">                    <span class="keyword">for</span>(<span class="type">int</span> k=<span class="number">2000</span>;k&gt;=a[j];k--)&#123;</span><br><span class="line">                        dp[k]|=dp[k-a[j]];</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="type">int</span> cnt=<span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> k=<span class="number">1</span>;k&lt;=<span class="number">2000</span>;k++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(dp[k])&#123;</span><br><span class="line">                    cnt++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            ans=<span class="built_in">max</span>(ans,cnt);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>,ans);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>时间复杂度是$O(2^n\times n \times \max{a_i})$<del>仍然非常高，但比之前的不知道低到哪里去了</del>，足以通过本题。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/08/06/%E3%80%8C%E7%8A%B6%E5%8E%8BDP%E3%80%8DP1441%20%E9%A2%98%E8%A7%A3%20%E7%A0%9D%E7%A0%81%E7%A7%B0%E9%87%8D/" data-id="clk9o8ln300068wi5hys49gre" data-title="「状压DP」P1441 题解 砝码称重" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/08/06/%E7%8A%B6%E6%80%81%E5%8E%8B%E7%BC%A9%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92(%E7%8A%B6%E5%8E%8BDP)%E8%AF%A6%E8%A7%A3/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          状态压缩动态规划(状压DP)详解
        
      </div>
    </a>
  
  
    <a href="/2020/08/05/%E6%B5%8B%E8%AF%95/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">测试</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>